package com.easyctba.core.controller;

import java.util.List;

import com.easyctba.core.domain.bo.StConfigBo;
import com.easyctba.core.domain.bo.StEncodeBo;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.easyctba.common.idempotent.annotation.RepeatSubmit;
import com.easyctba.common.log.annotation.Log;
import com.easyctba.common.web.core.BaseController;
import com.easyctba.common.mybatis.core.page.PageQuery;
import com.easyctba.common.core.domain.R;
import com.easyctba.common.core.validate.AddGroup;
import com.easyctba.common.core.validate.EditGroup;
import com.easyctba.common.log.enums.BusinessType;
import com.easyctba.common.excel.utils.ExcelUtil;
import com.easyctba.core.domain.vo.CtbaStVo;
import com.easyctba.core.domain.bo.CtbaStBo;
import com.easyctba.core.service.ICtbaStService;
import com.easyctba.common.mybatis.core.page.TableDataInfo;

/**
 * st分析
 * 前端访问路由地址为:/core/st
 *
 * @author LionLi
 * @date 2024-02-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/st")
public class CtbaStController extends BaseController {

    private final ICtbaStService ctbaStService;

    /**
     * 查询st分析列表
     */
    @SaCheckPermission("core:st:list")
    @GetMapping("/list")
    public TableDataInfo<CtbaStVo> list(CtbaStBo bo, PageQuery pageQuery) {
        return ctbaStService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出st分析列表
     */
    @SaCheckPermission("core:st:export")
    @Log(title = "st分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CtbaStBo bo, HttpServletResponse response) {
        List<CtbaStVo> list = ctbaStService.queryList(bo);
        ExcelUtil.exportExcel(list, "st分析", CtbaStVo.class, response);
    }

    /**
     * 获取st分析详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("core:st:query")
    @GetMapping("/{id}")
    public R<CtbaStVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(ctbaStService.queryById(id));
    }

    /**
     * 新增st分析
     */
    @SaCheckPermission("core:st:add")
    @Log(title = "st分析", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CtbaStBo bo) {
        return toAjax(ctbaStService.insertByBo(bo));
    }

    /**
     * 修改st分析
     */
    @SaCheckPermission("core:st:edit")
    @Log(title = "st分析", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CtbaStBo bo) {
        return toAjax(ctbaStService.updateByBo(bo));
    }

    /**
     * 删除st分析
     *
     * @param ids 主键串
     */
    @SaCheckPermission("core:st:remove")
    @Log(title = "st分析", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(ctbaStService.deleteWithValidByIds(List.of(ids), true));
    }
    /**
     * 根据项目id获取st分析信息
     * @param id 项目id
     * @return VO对象
     */
    @GetMapping("/v1/{id}")
    public R<CtbaStVo> getInfoByUserAndProject(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return ctbaStService.getInfoByUserAndProject(id);
    }

    /**
     * 初始化ST分析，设置编码间隔和编码视频的起止时间，并修改状态以进入下一步
     */
    @PostMapping("/stConfig")
    public R<Void> initSt(@Validated(EditGroup.class) @RequestBody StConfigBo bo){
        return ctbaStService.stConfig(bo);
    }
    /**
     * 编码st
     */
    @PostMapping("/encode/{id}")
    public R<Void> encode(@Validated(EditGroup.class) @RequestBody StEncodeBo bo,@PathVariable Long id){
        return ctbaStService.encode(bo,id);
    }

    /**
     * 上一步
     * @param id st分析id
     */
    @PutMapping("/pre/{id}")
    public R<Void> pre(@NotNull(message = "主键不能为空") @PathVariable Long id){
        return ctbaStService.pre(id);
    }

    /**
     * 完成分析
     * @param id st分析id
     */
    @PutMapping("/finish/{id}")
    public R<Void> finish(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return ctbaStService.finish(id);
    }
}
